package de.gruessing.gwtsports.server.services;

import java.io.File;

import javax.xml.bind.JAXBElement;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.gruessing.gwtsports.client.services.DataRetriever;
import de.gruessing.gwtsports.server.data.OriginalRootData;
import de.gruessing.gwtsports.server.data.tcdata.TrainingCenterDatabaseT;
import de.gruessing.gwtsports.server.xmlmapper.TCXMapper;
import de.gruessing.gwtsports.server.xmlmapper.XMLMapper;
import de.gruessing.gwtsports.server.xmlparser.XMLParser;
import de.gruessing.gwtsports.shared.mappeddata.tcdata.TrainingDataDTO;

public class DataRetrieverImpl extends RemoteServiceServlet implements DataRetriever
{	
	@Override
	public TrainingDataDTO getTrainingData(String in_strTCXFilePath)
	{
		// Determine Format
		// Parse with correct parser
		// Map to TrainingDataDTO
		try
		{
			File xXML = new File(in_strTCXFilePath);
			XMLParser xParser = XMLParserFactory.getParser( XMLParserFactory.TCXPARSER );
			
			JAXBElement<TrainingCenterDatabaseT> xGarminTrainData = (JAXBElement<TrainingCenterDatabaseT>) xParser.parseXML(xXML);
			OriginalRootData xTCData = xGarminTrainData.getValue();
			
			XMLMapper xMapper = new TCXMapper();
			return xMapper.mapTrainingData( xTCData );
		}
		catch( ServiceNotAvailableException ex ) 
		{
			ex.printStackTrace();
			return null;
		}
	}
}